import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'
import {Message} from 'element-ui'
import common from "../api/common";

Vue.use(Router)

var router = new Router({
    // mode: 'history',
    routes: [
      { path: '*', component:(resolve)=>require(['components/notPage/notPage'],resolve) },
      {path: '/static/login', name: 'login', component:(resolve)=>require(['components/login'],resolve)},
      {path: '/static/user/:tencentId', name: 'user', component:(resolve)=>require(['components/user'],resolve),
        /*meta: {
          requiresAuth: true, allowBack: false
        }*/
      },
      {path: '/static/register', name: 'register', component:(resolve)=>require(['components/register'],resolve)},
      {path: '/static/aboutUs', name: 'aboutUs', component:(resolve)=>require(['components/aboutUs'],resolve)},
      {path: '/static/systemList', name: 'systemList', component:(resolve)=>require(['components/systemList'],resolve)},
      {path: '/', redirect: '/static/user' }
    ]
})

router.beforeEach((to, from, next) => {
  if(to.params.tencentId){
    sessionStorage.setItem('tencentId',to.params.tencentId)
  }
  const isLogin = store.state.user.openid
  let path=to.path.split('/').splice(0,3)//去掉路由带参数
  let url = path.join('/')
  if (to.matched.some(record => record.meta.requiresAuth) && (!isLogin||isLogin=='')) {
    if(!to.name.includes('login')){
      sessionStorage.setItem('lastPath',to.path)// 记录上一次路径
    }
    next({path: '/static/login'})
  } else {
    next()
  }
})
export default router
